package com.clarence.dada.modular.system.user.controller;

import com.clarence.dada.core.auth.context.LoginContext;
import com.clarence.dada.core.auth.entity.LoginUser;
import com.clarence.dada.core.auth.entity.dto.LoginDto;
import com.clarence.dada.core.auth.service.AuthService;
import com.clarence.dada.core.base.Result;
import com.clarence.dada.core.factory.UserLoginInfoFactory;
import com.clarence.dada.modular.system.user.entity.req.LoginReq;
import com.clarence.dada.modular.system.user.entity.resp.CurrentUserInfoResp;
import com.clarence.dada.modular.system.user.entity.resp.LoginResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 登录登出控制器
 * <p>
 *
 * @author <a href="mailto:1093776034@qq.com">GD</a>
 * @since 2023/12/30 23:05
 */
@Slf4j
@RestController
@Tag(name = "登陆登出管理", description = "登陆登出管理Controller")
public class LoginController {
    @Resource
    private AuthService authService;

    /**
     * 用户登陆
     *
     * @author GD
     * @since 2021/3/17 17:23
     */
    @Operation(summary = "登陆", description = "用户登陆")
    @PostMapping("/loginApi")
    public Result<LoginDto> loginApi(@RequestBody @Validated LoginReq req) {
        req.setCreateCookie(false);
        return new Result<>(authService.login(req));
    }


    /**
     * 用户登出
     *
     * @author GD
     * @since 2021/3/17 17:24
     */
    @Operation(summary = "登出", description = "用户登陆")
    @PostMapping("/logoutAction")
    public Result<Void> logoutAction() {
        authService.logout();
        return Result.SUCCESS;
    }


    /**
     * 获取当前用户的用户信息
     *
     * @author fengshuonan
     * @date 2021/3/17 17:37
     */
    @Operation(summary = "获取当前用户的用户信息", description = "获取当前用户的用户信息")
    @PostMapping("/getCurrentLoginUserInfo")
    public Result<CurrentUserInfoResp> getCurrentLoginUserInfo() {
        LoginUser loginUser = LoginContext.me().getLoginUser();

        // 转化返回结果
        CurrentUserInfoResp currentUserInfoResp = UserLoginInfoFactory.parseUserInfo(loginUser);

        return new Result<>(currentUserInfoResp);
    }
}
